package edu.fjnu.order.view.adapter;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

@SuppressWarnings("serial")
public class JListCell extends JPanel implements ListCellRenderer {

	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		text = value.toString();
		background = isSelected ? list.getSelectionBackground() : list.getBackground();
		foreground = isSelected ? list.getSelectionForeground() : list.getForeground();
		return this;
	}
	
	public void paintComponent(Graphics g) {
		
       g.setColor(background);
       g.fillRect(0, 0, getWidth(), getHeight());  //设置背景色
       g.setColor(foreground);
       FontMetrics fm = g.getFontMetrics();
       char [] data = text.toCharArray();
       int halfLen = fm.charsWidth(data, 0, data.length) / 2;
       g.drawString(text, 75 - halfLen, 15);   //在制定位置绘制文本
    }

    public Dimension getPreferredSize() {
    	//TODO 大小 硬编码 
    	return new Dimension(150, 25);   //Cell的尺寸
    }
	
	private String text;
    private Color background;
    private Color foreground;
}
